//
//  BNDConstance.h
//  BNDAnnouncement
//
//  Created by pie on 2022/3/28.
//  Copyright © 2022 LaKa. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BNDCommonMarcos.h"


#pragma mark - Block

typedef BOOL (^BoolBlock)(void);
typedef int  (^IntBlock) (void);
typedef id _Nullable   (^IDBlock)  (void);

typedef void (^VoidBlock_int)(int);
typedef BOOL (^BoolBlock_int)(int);
typedef int  (^IntBlock_int) (int);
typedef id _Nullable   (^IDBlock_int)  (int);

typedef void (^VoidBlock_string)(NSString *_Nullable);
typedef BOOL (^BoolBlock_string)(NSString *_Nullable);
typedef int  (^IntBlock_string) (NSString *_Nullable);
typedef id _Nullable   (^IDBlock_string)  (NSString *_Nullable);

typedef void (^VoidBlock)(void);
typedef void (^VoidBlock_id)(id _Nullable );
typedef BOOL (^BoolBlock_id)(id _Nullable );
typedef int  (^IntBlock_id) (id _Nullable );
typedef id _Nullable   (^IDBlock_id)  (id _Nullable );



#pragma mark - 网络配置

UIKIT_EXTERN NSString * _Nonnull const SERVER_URL;

UIKIT_EXTERN NSString * _Nonnull const SERVER_IN_URL;

UIKIT_EXTERN NSString * _Nonnull const SERVER_UPLOAD_URL;


// MARK: -- 通知 --

UIKIT_EXTERN NSString * _Nonnull const kNotificationRefreshSuccess; // 刷新通知

#pragma mark - 配置

#define BND_BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); };

#define BND_dispatch_main_sync_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_sync(dispatch_get_main_queue(), block);\
}

#define BND_dispatch_main_async_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}

// 设置图片
#define BNDImageNamed(__imageName) [BNDPreference getLocalImage:__imageName]


// 是否为空对象
#define BNDObjectIsNil(__object)  ((nil == __object) || [__object isKindOfClass:[NSNull class]])

// 字符串为空
#define BNDStringIsEmpty(__string) ((__string.length == 0) || BNDObjectIsNil(__string))

// 字符串不为空
#define BNDStringIsNotEmpty(__string)  (!BNDStringIsEmpty(__string))

// 数组为空
#define BNDArrayIsEmpty(__array) ((BNDObjectIsNil(__array)) || (__array.count==0))

///// AppDelegate
//#define BNDSharedAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

//  通知中心
#define BNDNotificationCenter [NSNotificationCenter defaultCenter]

#define WS(weakSelf) __weak __typeof(&*self) weakSelf = self;

#pragma mark - 设置宽高

#define kDeviceHeight [UIScreen mainScreen].bounds.size.height
#define kDeviceWidth  [UIScreen mainScreen].bounds.size.width

//宏定义屏幕大小的比例
#define kDeviceRatio    (kDeviceWidth / 375.0)

#pragma mark - 文件处理

/// 项目重要数据备份的文件夹名称（Documents/WeChatDoc）利用NSFileManager来访问
#define BND_DOC_NAME  @"BNDDoc"

/// 项目轻量数据数据备份的文件夹（Library/Caches/WeChatCache）利用NSFileManager来访问
#define BND_CACHE_NAME  @"BNDCache"

// AppCaches 文件夹路径
#define BNDCachesDirectory [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
// App DocumentDirectory 文件夹路径
#define BNDDocumentDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject]

static inline BOOL NSStringIsEmpty(NSString * _Nullable str)
{
    return (str == nil || [@"" isEqualToString:str]);
}

static inline BOOL TTObjectIsNil(id _Nullable  obj) {
    return (obj ? NO : YES);
};

static inline NSString * _Nullable TTStringNotNil(NSString * _Nullable   string) {
    return (string ? string : @"");
};

static inline NSString * _Nullable TTStringNilToZero(NSString * _Nullable  string) {
    if (!string) {
        string = @"0";
        return string;
    }
    return string;
};

//static inline UIImage * _Nullable BNDImage(NSString * _Nullable  string)
//{
//    if (string.length>0) {
//        
//        NSBundle *currentBundle = [NSBundle bundleForClass:[[BNDPushVCHandler topViewController] class]];
//        
//                NSURL *url = [currentBundle URLForResource:@"OfficeDocSdk" withExtension:@"bundle"];
//                NSBundle *targetBundle = [NSBundle bundleWithURL:url];
//            
//            UIImage * image =[UIImage imageNamed:string
//                                  inBundle:targetBundle
//             compatibleWithTraitCollection:nil];
//    
//        return image;
//    }
//    return nil;
//};

/// 辅助方法 创建一个文件夹
static inline void SYCreateDirectoryAtPath(NSString * _Nullable path){
    BOOL isDir = NO;
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
    if (isExist) {
        if (!isDir) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        }
    } else {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
}
/// 重要数据备份的文件夹路径，通过NSFileManager来访问
static inline NSString * _Nullable SYSeeYuDocDirPath(){
    return [BNDDocumentDirectory stringByAppendingPathComponent:BND_DOC_NAME];
}
/// 通过NSFileManager来获取指定重要数据的路径
static inline NSString * _Nullable SYFilePathFromSeeYuDoc(NSString * _Nonnull filename){
    NSString *docPath = SYSeeYuDocDirPath();
    SYCreateDirectoryAtPath(docPath);
    return [docPath stringByAppendingPathComponent:filename];
}

/// 微信轻量数据备份的文件夹路径，通过NSFileManager来访问
static inline NSString * _Nullable SYSeeYuCacheDirPath(){
    return [BNDCachesDirectory stringByAppendingPathComponent:BND_CACHE_NAME];
}
/// 通过NSFileManager来访问 获取指定轻量数据的路径
static inline NSString * _Nullable SYFilePathFromSeeYuCache(NSString * _Nullable filename){
    NSString *cachePath = SYSeeYuCacheDirPath();
    SYCreateDirectoryAtPath(cachePath);
    return [cachePath stringByAppendingPathComponent:filename];
}


#pragma mark - 颜色转换

#define RGB(r,g,b)          [UIColor colorWithRed:(r)/255.f \
                                            green:(g)/255.f \
                                             blue:(b)/255.f \
                                            alpha:1.f]

#define BND_VIEW_BG_COLOR           UIColorWithHex(0xf5f5f5)

static inline UIColor * _Nonnull UIColorWithHexAndAlpha(long hexColor, float opacity)
{
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
}

static inline UIColor * _Nonnull UIColorHexAndAlpha(long hexColor, float opacity)
{
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
}

static inline UIColor * _Nullable UIColorWithHex(long hexColor)
{
    return UIColorWithHexAndAlpha(hexColor, 1.f);
}


#pragma mark - PingFang字体

static inline UIFont * _Nullable BNDPFRegularFont(CGFloat fontSize)
{
    UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:fontSize];
    if (!font) {
        font = [UIFont systemFontOfSize:fontSize];
    }
    
    return font;
}

static inline UIFont * _Nullable  BNDPFMediumFont(CGFloat fontSize)
{
    UIFont *font = [UIFont fontWithName:@"PingFangSC-Medium" size:fontSize];
    if (!font) {
        font = [UIFont systemFontOfSize:fontSize];
    }
    
    return font;
}

static inline UIFont * _Nullable BNDPFHeavyFont(CGFloat fontSize)
{
    UIFont *font = [UIFont fontWithName:@"PingFangSC-Semibold" size:fontSize];
    if (!font) {
        font = [UIFont boldSystemFontOfSize:fontSize];
    }
    
    return font;
}

static inline CGFloat BNDNavigationBarHeight()
{
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    return  rectStatus.size.height+44;
}

static inline UIEdgeInsets LKSafeAreaInsets() {
    
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    if (@available(iOS 11.0, *)) {
        edgeInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
    }
    return edgeInsets;
}

static inline CGFloat BNDSafeAreaBottomHeight() {
    
    static CGFloat safeAreaBottomHeight = 0;
    
    if (@available(iOS 11.0, *)) {
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            safeAreaBottomHeight = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
        });
    }
    return safeAreaBottomHeight;
}

static inline CGFloat BNDTabBarHeight() {
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    if(rectStatus.size.height >= 44) {
        return 83;
    }else {
        return 49;
    }
}

static inline CGFloat BNDStatusBarHeight(){
    float statusBarHeight = 0;
    if (@available(iOS 13.0, *)) {
    UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
    statusBarHeight = statusBarManager.statusBarFrame.size.height;
    }
    else {
    statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    }
    return statusBarHeight;
}


